﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Controller.Enum;
using Model;
using Newtonsoft.Json.Linq;

namespace Controller.Helper
{
    public class ProductGrp
    {
        public static string GetProductCate()
        {
            using (DCSalePriceDataContext nawaDataContext = new DCSalePriceDataContext())
            {
                var allProductCate = from prodcate in nawaDataContext.viewProductCates

                                     
                                     select prodcate;

                JObject k = JObject.FromObject(new
                                                   {
                                                       AllProduct = allProductCate
                                                   });
                return k.ToString();
            }
        }
        public  static string GetProductID(string productName)
        {
            using (DCSalePriceDataContext nawaDataContext = new DCSalePriceDataContext())
            {
                var allProductCate = from prodcate in nawaDataContext.viewProductCates
                                     where prodcate.mprodh_grpname.Equals(productName)
                                     select Convert.ToInt32(prodcate.mprodh_grpID);

                if (allProductCate.ToArray().Length > 0)
                {
                    return allProductCate.ToArray()[0].ToString();
                }
                else
                {
                    return "";
                }
                
            }
        }

        public static string GetProductCate(bool isAll)
        {
            using (DCSalePriceDataContext nawaDataContext = new DCSalePriceDataContext())
            {
                var allProductCate = from prodcate in nawaDataContext.viewProductCates

                                     select prodcate;

                JObject k = JObject.FromObject(new
                {
                    AllProduct = allProductCate
                });
                return k.ToString();
            }
        }

        public static string GetAllProdHier()
        {
            using (DCSalePriceDataContext nawaDataContext = new DCSalePriceDataContext())
            {
                var allProdHier = from allprodhier in nawaDataContext.tblProductHiers
                                  select allprodhier;
                JObject k = JObject.FromObject(new
                {
                    AllProdHier = allProdHier
                });
                return k.ToString();
            }
        }

        public static int ProdHierAction(List<tblProductHier> productHiers, EnumAction action)
        {
            using (DCSalePriceDataContext nawaDataContext = new DCSalePriceDataContext())
            {
                if (productHiers != null && productHiers.Count > 0)
                {
                    switch (action)
                    {
                        case EnumAction.Update:
                            try
                            {
                                foreach (tblProductHier newprodhier in productHiers)
                                {
                                    tblProductHier oldprodhier = nawaDataContext.tblProductHiers.Single(prodhl => prodhl.mprodh_levelno == newprodhier.mprodh_levelno);
                                    if (string.IsNullOrEmpty(newprodhier.mprodhlName))
                                    {
                                        oldprodhier.mprodhlName = newprodhier.mprodhlName;
                                    }
                                    if (string.IsNullOrEmpty(newprodhier.mprodhlSAP))
                                    {
                                        oldprodhier.mprodhlSAP = newprodhier.mprodhlSAP;
                                    }
                                    if (string.IsNullOrEmpty(newprodhier.mprodh_length))
                                    {
                                        oldprodhier.mprodh_length = newprodhier.mprodh_length;
                                    }
                                    oldprodhier.isDisable = newprodhier.isDisable;
                                }
                                nawaDataContext.SubmitChanges();
                                return productHiers.Count;
                            }
                            catch (Exception ex)
                            {

                                return 0;
                            }
                        case EnumAction.Insert:
                            try
                            {
                                foreach (tblProductHier prodhl in productHiers)
                                {
                                    nawaDataContext.tblProductHiers.InsertOnSubmit(prodhl);
                                }
                                nawaDataContext.SubmitChanges();
                                return productHiers.Count;

                            }
                            catch (Exception ex)
                            {
                                return -1;
                            }
                        case EnumAction.Delete:
                            try
                            {
                                var prodhier1 = from deleteprodhl in nawaDataContext.tblProductHiers
                                            where productHiers.Contains(deleteprodhl)
                                            select deleteprodhl;
                                //dataContext.tblUsers.DeleteByPK<tblUser>(users.AsQueryable());
                                int deletedRole = nawaDataContext.tblProductHiers.DeleteBatch<tblProductHier>(prodhier1);
                                //dataContext.tblUsers.UpdateBatch<tblUser>(users1, p => new tblUser{ Email = p.DateCreated, 
                                //           UserName = "Chris Cavanagh" } );
                                //dataContext.tblUsers.DeleteAllOnSubmit(users);
                                //dataContext.SubmitChanges();
                                return deletedRole;
                            }
                            catch (Exception)
                            {

                                return 0;
                            }
                    }
                }
                else
                {
                    return 0;
                }
            }
            return 0;
        }

        public static int ProdGrpAction(List<viewProductCate> pgs, EnumAction action)
        {
            using (DCSalePriceDataContext nawaDataContext = new DCSalePriceDataContext())
            {
                if (pgs != null && pgs.Count > 0)
                {
                    switch (action)
                    {
                        case EnumAction.Update:
                            try
                            {
                                foreach (viewProductCate newprodgrp in pgs)
                                {
                                    tblProductCate oldProdGrp = nawaDataContext.tblProductCates.Single(user => user.mprodh_grpID == newprodgrp.mprodh_grpID);
                                    //oldUser.UserName = newuser.UserName;
                                    //oldProdGrp.mprodh_grpname = newprodgrp.mprodh_grpname;
                                    //oldProdGrp.mprodh_levelno = newprodgrp.mprodh_levelno;
                                    //oldProdGrp.isDisable = newprodgrp.isDisable;
                                }
                                nawaDataContext.SubmitChanges();
                                return pgs.Count;

                            }
                            catch (Exception ex)
                            {

                                return 0;
                            }
                        case EnumAction.Insert:
                            try
                            {
                                foreach (viewProductCate pg in pgs)
                                {
                                    nawaDataContext.viewProductCates.InsertOnSubmit(pg);
                                }
                                nawaDataContext.SubmitChanges();
                                return pgs.Count;

                            }
                            catch (Exception ex)
                            {

                                return 0;
                            }
                        case EnumAction.Delete:
                            try
                            {
                                var pg1 = from deletepgs in nawaDataContext.viewProductCates
                                          where pgs.Contains(deletepgs)
                                          select deletepgs;
                                //int kkk = dataContext.tblUsers.DeleteByPK<tblUser>(users);
                                int result = nawaDataContext.viewProductCates.DeleteBatch<viewProductCate>(pg1);
                                //dataContext.tblUsers.UpdateBatch<tblUser>(users1, p => new tblUser{ Email = p.DateCreated, 
                                //           UserName = "Chris Cavanagh" } );
                                //dataContext.tblUsers.DeleteAllOnSubmit(users);
                                //dataContext.SubmitChanges();
                                return result;
                            }
                            catch (Exception)
                            {

                                return 0;
                            }
                    }
                }
                else
                {
                    return 0;
                }
            }
            return 0;
        }
    }
}
